package com.yxjme.ecgchart;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import com.yxjme.egcviewlibrary.manager.CameraHeartManager;
import com.yxjme.egcviewlibrary.view.ECGView2;
import com.yxjme.egcviewlibrary.view.ECGAllDataView;
import com.yxjme.egcviewlibrary.view.ECGView;
import com.yxjme.egcviewlibrary.view.ECGView1;
import java.util.ArrayList;


public class HeartRateActivity extends AppCompatActivity {

    private  SurfaceView preview = null;
    private ECGView ecgView;
    private ECGAllDataView mECG_allData_View;
    private ArrayList<String> dataList = new ArrayList<>();
    private ECGView1 mECGView1;
    private SensorManager sensorManager;
    private Sensor mSensor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heart_rate);

        if (ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) !=
                PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{
                    Manifest.permission.CAMERA
            },0);
        }else {
            init();
        }


        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

        mCeView = findViewById(R.id.mCeView);
        mCeView.setTemp_x(30);
        mCeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCeView.reset();
            }
        });
    }


    ECGView2 mCeView;


    @Override
    protected void onResume() {
        super.onResume();
        CameraHeartManager.newInstance().start();
        sensorManager.registerListener(sensorEventListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }


    SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            float[] its = event.values;
            //Log.d(TAG,"its array:"+its+"sensor type :"+event.sensor.getType()+" proximity type:"+Sensor.TYPE_PROXIMITY);
            if (its != null && event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
//            System.out.println("its[0]:" + its[0]);
//            tv.setText(its[0]+"");
                //经过测试，当手贴近距离感应器的时候its[0]返回值为0.0，当手离开时返回1.0
                if (its[0] == 0.0) {// 贴近手机
                    Log.d("hands","hands up in calling activity");
                } else {// 远离手机
                    Log.d("hands","hands moved in calling activity");
                }
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };


    @Override
    protected void onPause() {
        super.onPause();
        CameraHeartManager.newInstance().stop();


        sensorManager.unregisterListener(sensorEventListener);
    }



    private void init() {
        //获取SurfaceView控件
        preview = (SurfaceView) findViewById(R.id.preview);
        ecgView = findViewById(R.id.ecg_data_ecgView);
        ecgView.setMode(ECGView.MODE.RIGHT_TO_LEFT);
        ecgView.isSupportGrid_ver(false);
        ecgView.isSupportShiXian(false);
        ecgView.setLineWidth(10);
        ecgView.setLineWidth(10);
        ecgView.setPathColor(Color.GREEN);
        ecgView.setLineColor(Color.argb(100,255,255,255));


        mECGView1 = findViewById(R.id.mECGView1);
        mECGView1.setMode(ECGView.MODE.RIGHT_TO_LEFT);
        mECGView1.setSupportScroll(true);
        mECGView1.setGap_x_count(30);
        mECG_allData_View = findViewById(R.id.mECG_allData_View);


        CameraHeartManager.newInstance()
                .setSurfaceView(preview)
                .addDrawHeartListener(addDrawHeartListener)
                .init();

        findViewById(R.id.tv_Val)
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dataList.clear();
                        ecgView.clear();
                        mECG_allData_View.clear();
                    }
                });

        findViewById(R.id.finish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mECG_allData_View.setData(dataList);
            }
        });
    }

    private CameraHeartManager.AddDrawHeartListener addDrawHeartListener = new CameraHeartManager.AddDrawHeartListener() {

        @Override
        public void onDraw() {
            dataList.add(CameraHeartManager.newInstance().getHeartRateVal());
            if (ecgView!=null){
                ecgView.setData(dataList);
                mECGView1.setData(dataList);
            }

            if (isCanDraw())
            mCeView.setData(CameraHeartManager.newInstance().getHeartRateVal());
        }

        @Override
        public void onDown() {
            Log.v("=======aa=====","onDown");
        }

        @Override
        public void onUp() {
            Log.v("=======aa=====","onUp");
        }
    };


    /**
     * 是否能绘制点     */
    private long lastTime;
    public boolean isCanDraw(){
        if (System.currentTimeMillis()-lastTime>=500){
            lastTime = System.currentTimeMillis() ;
            return true ;
        }
        return false ;
    }

}


